package cpre556.project2.eclipsePlugin.support;

import java.io.*;
import java.util.Vector;

/**
 * Wrapper for its4
 */
public class ITS4toXML extends Plugin{
	/**
	 * File reader
	 */
	private BufferedReader reader = null;
	
	/**
	 * Plug-in name
	 */
	protected String name = "its4";
	
	/**
	 * Constructor
	 */
	public ITS4toXML(){
		super();
		
		String location = settings.getPlugin(name);
		
		if(location.equals("")){
			settings.saveSetting(name, saver.getExe());
		}
	}
	
	/**
	 * Parses the output of ITS4
	 */
	protected void parseFile(){
		try{
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(saver.getTxt())));
			
			vulnerabilityList = new Vector<Vulnerability>();
			Vulnerability tempVulnerability;
			Vector<Vulnerability> tempVulnerabilityList = new Vector<Vulnerability>();
			
			String inLine;
			while((inLine = reader.readLine()) != null){					
				if(inLine.indexOf(":") != -1){
					boolean loop = true;
					// Set the filename, line number, priority and function name
					file:
					while(inLine.indexOf(":") != -1 && loop){
						tempVulnerability = new Vulnerability();
						
						if(inLine.startsWith("Potential race")){
							loop = false;
							break file;
						}
						
						tempVulnerability.setFilename(inLine.substring(0, inLine.toLowerCase().indexOf(".c:") + 2));						
						tempVulnerability.setLineNumber(Integer.valueOf(inLine.substring(inLine.indexOf(".c:") + 3, inLine.lastIndexOf(":"))).intValue());
						tempVulnerability.setPriority(inLine.substring(inLine.indexOf("(") + 1, inLine.lastIndexOf(")")));
						tempVulnerability.setFunctionName(inLine.substring(inLine.lastIndexOf(" ")).trim());				
						tempVulnerabilityList.add(tempVulnerability);
						
						inLine = reader.readLine();
						inLine = reader.readLine();
					}
					
					String desc = inLine;
					
					if(inLine.startsWith("Potential race")){
						desc += reader.readLine();
						
						String line = "";
						
						do{
							line = reader.readLine();
							
							if(line.equals("")){
								reader.mark(180);
								
								line = reader.readLine();
								
								if(line.indexOf(" ") != -1){
									reader.reset();
								}
								else{
									desc += line;
									line = ":";
								}
							}
							else{
								desc += line;
							}
						}
						while(line.indexOf(":") != -1 && line != null);
					}
					else{
						while (desc.indexOf(".") == -1){
							desc = desc + reader.readLine();
						}
						inLine = reader.readLine();
					}
					
					String sugg = reader.readLine();
					
					while (sugg.indexOf(".") == -1){
						sugg = sugg + reader.readLine();
					}
					
					int tempSize = tempVulnerabilityList.size();
					
					for(int i = 0; i < tempSize; i++){							
						tempVulnerabilityList.get(i).setDescription(desc);
						tempVulnerabilityList.get(i).setSuggestion(sugg);
						
						vulnerabilityList.add(tempVulnerabilityList.get(i));
					}								
					
					tempVulnerabilityList.clear();					
				}									
			}
			reader.close();
		}
		catch(FileNotFoundException fnfe){		
			fnfe.printStackTrace();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
}
